package com.itheima.reggie.common;

/*Mybatis Plus公共字段自动填充，也就是在插入或者更新的时候为指定字段赋予指定的值，使用它的好处就是可以
        统一对这些字段进行处理，避免了重复代码。
        实现步骤：
        1、在实体类的属性上加入@TableField注解，指定自动填充的策略
        2、按照框架要求编写元数据对象处理器，在此类中统一为公共字段赋值，此类需要实现MetaObjectHandler接口*/

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {


    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("公共字段自动填充[insert]:{}",metaObject);
        metaObject.setValue("createTime", LocalDateTime.now());
        metaObject.setValue("updateTime",LocalDateTime.now());
        metaObject.setValue("createUser",BaseContext.getCurrentId());
        metaObject.setValue("updateUser",BaseContext.getCurrentId());
//        this.fillStrategy(metaObject, "createTime", LocalDateTime.now());
//        this.fillStrategy(metaObject, "updateTime", LocalDateTime.now());
//        this.fillStrategy(metaObject, "createUser", BaseContext.getCurrentId());
//        this.fillStrategy(metaObject, "updateUser", BaseContext.getCurrentId());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("公共字段填充[update]:{}",metaObject);
        metaObject.setValue("updateTime",LocalDateTime.now());
        metaObject.setValue("updateUser",BaseContext.getCurrentId());
//        this.fillStrategy(metaObject, "updateTime", LocalDateTime.now());
//        this.fillStrategy(metaObject, "updateUser", BaseContext.getCurrentId());
    }
}
